<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 积分兑换记录控制器
 * @package app\index\controller
 */
class ViperIntegralLog extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取积分兑换记录列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $sql = Db::name('qt_viper_integral_log')
                ->field('l.*,s.name as store_name,t.name as type_name,u.real_name,ie.name as template_name')
                ->alias('l')
                ->leftJoin('qt_store s','s.id = l.store_id')
                ->leftJoin('qt_viper_type t','t.id = l.viper_type_id')
                ->leftJoin('qt_users u','u.id = l.user_id')
                ->leftJoin('qt_viper_integral_exchange ie','ie.id = l.integral_id')
                ->where(['l.store_id' => $userInfo['store_id']]);
            //时间筛选
            if(isset($getData['start_time']) && !empty($getData['start_time']) && isset($getData['end_time']) && !empty($getData['end_time'])){
                $start_time = strtotime($getData['start_time']);
                $end_time = strtotime($getData['end_time']);
                $sql = $sql->whereBetweenTime('l.create_time',$start_time,$end_time);
            }
            //会员卡类型筛选
            if (isset($getData['viper_type_id']) && !empty($getData['viper_type_id']))
            {
                $sql = $sql->where("FIND_IN_SET(" . $getData['viper_type_id'] . ",l.viper_type_id)");
            }
            //电话或者卡号筛选
            if (isset($getData['number']) && !empty($getData['number']))
            {
                $map1[] = ['l.mobile','eq',$getData['number']];
                $map2[] = ['l.card_no','eq',$getData['number']];
                $sql = $sql->whereOr([$map1,$map2]);
            }
            $data = $sql->order('l.id desc')->paginate($getData['limit']);
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 编辑积分兑换记录
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            unset($getData['create_time']);
            unset($getData['update_time']);
            $update= Db::name('qt_viper_integral_log')->where('id',$getData['id'])->update($getData);
            if($update){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑积分兑换记录
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_viper_integral_log')
                ->field('l.*,s.name as store_name,t.name as type_name,u.real_name')
                ->alias('l')
                ->leftJoin('qt_store s','s.id = l.store_id')
                ->leftJoin('qt_viper_type t','t.id = l.viper_type_id')
                ->leftJoin('qt_users u','u.id = l.user_id')
                ->where(['l.id' => $getData['id']])
                ->find();
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除积分兑换记录
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $log = Db::name('qt_viper_integral_log')
                ->field('l.*,c.user_id')
                ->alias('l')
                ->leftJoin('qt_viper_card c','l.card_no = c.card_no')
                ->where('l.id',$getData['id'])
                ->find();
            //dump($log);die;
            try{
                Db::startTrans();
                //删除记录
                Db::name('qt_viper_integral_log')->where('id',$getData['id'])->delete();
                //更新卡积分
                Db::name('qt_viper_card')->where('card_no',$log['card_no'])->setInc('score',$log['integral']);
                //更新余额
                Db::name('qt_viper_card')->where('card_no',$log['card_no'])->setInc('amount',$log['money']);
                //更新余额
                Db::name('qt_user_viper')->where('id',$log['user_id'])->setInc('money',$log['money']);
                //更新卡积分
                Db::name('qt_user_viper')->where('id',$log['user_id'])->setInc('score',$log['integral']);
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('删除失败','',204);
            }
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 查询会员积分信息
     */
    public function search(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['number']) || empty($getData['type']) || empty($getData['passwd'])){
                $this->result('缺少参数', '', 204);
            }
            $sql = Db::name('qt_viper_card')
                ->field('c.id as viper_card_id,c.name,t.name as type_name,score,c.passwd,c.salt')
                ->alias('c')
                ->leftJoin('qt_viper_type t','t.id = c.viper_type_id');
            if($getData['type'] == '1'){
                //手机号
                $sql = $sql->where('c.mobile',$getData['number']);
            }
            if($getData['type'] == '3'){
                //手机号
                $sql = $sql->where('c.card_no',$getData['number']);
            }
            $data = $sql->find();
            if(empty($data)){
                $this->result('该会员不存在', '', 204);
            }
            $this->result('查询成功', $data, 200);
//            $newPass = pwd($getData['passwd'],$data['salt']);
//            if($newPass == $data['passwd']){
//                $this->result('查询成功', $data, 200);
//            }
//            $this->result('密码不正确', $data, 200);
        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 添加积分
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['viper_card_id']) || empty($getData['integral_id']) || empty($getData['passwd'])){
                $this->result('缺少参数', '', 204);
            }
            $passwd = Db::name('qt_viper_card')->where(['id'=>$getData['viper_card_id']])->find();
            $newPass = pwd($getData['passwd'],$passwd['salt']);
            if($newPass != $passwd['passwd']){
                $this->result('密码错误', '', 204);
            }
            $cardRes = Db::name('qt_viper_card')
                ->field('id,viper_user_id,viper_type_id,mobile,name,card_no,score')
                ->where('id',$getData['viper_card_id'])
                ->find();
            $integralRes = Db::name('qt_viper_integral_exchange')
                ->field('id,integral,money,goods_name,vip_type_id')
                ->where('id',$getData['integral_id'])
                ->find();
            if($cardRes['viper_type_id'] != $integralRes['vip_type_id']){
                $this->result('会员卡类型错误，暂时不能兑换', '', 204);
            }

            if($cardRes['score'] < $integralRes['integral']){
                $this->result('积分不够，暂时不能兑换', '', 204);
            }
            $insert = [
                'store_id'=>$this->userInfo['store_id'],
                'viper_type_id'=>$cardRes['viper_type_id'],
                'viper_card_id'=>$getData['viper_card_id'],
                'mobile'=>$cardRes['mobile'],
                'name'=>$cardRes['name'],
                'card_no'=>$cardRes['card_no'],
                'integral'=>$integralRes['integral'],
                'money'=>$integralRes['money'],
                'goods_name'=>$integralRes['goods_name'],
                'integral_id'=>$getData['integral_id'],
                'user_id'=>$this->userInfo['id'],
                'create_time'=>time(),
            ];
            try{
                Db::startTrans();
                //插入积分兑换记录
                Db::name('qt_viper_integral_log')->insert($insert);
                //更新会员卡积分
                Db::name('qt_viper_card')->where('id',$getData['viper_card_id'])->setDec('score',$integralRes['integral']);
                //有金额的加金额
                if($integralRes['money'] != null){
                    Db::name('qt_viper_card')->where('id',$getData['viper_card_id'])->setInc('amount',$integralRes['money']);
                }
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('兑换失败','',204);
            }
            $this->result('兑换成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
